#include <assert.h>
#include <qlabel.h>
#include <qtextedit.h>
#include <qtimer.h>
#include <qfile.h>
#include <qlineedit.h>
#include <tws_debugging.h>
#include <xwritelogexport.h>
#include <xinifile.h>
#include "xqueuewidget.h"
#include "infomatcommon.h"
#include "xdatabootconf.h"

XQueueWidget::XQueueWidget(QWidget* parent, Qt::WFlags fl)
: QWidget(parent,Qt::FramelessWindowHint | Qt::WStyle_StaysOnTop),
	m_queueNumber(0),m_isShowQno(false),
	m_counter(0), m_layout(0)
{
	XDataBootConf  xbootconf;
	m_layout = xbootconf.getLayout(); 
	m_lbQueue = new QLabel(this);
	m_lbLogo  = new QLabel(this);  //for layout
	m_lbSmallQueue = new QLabel(this); 
	m_timer   = new QTimer(this);
	connect (m_timer, SIGNAL(timeout()), this, SLOT(slotSwitchBackground()));
	m_timer->stop();
	this->setupUi();
}

XQueueWidget::~XQueueWidget()
{ 
}

int XQueueWidget::setupUi()  /* {{{ */
{
	assert (0==m_iniLayout.open(k_ini_file_queue));
	m_lblGreenBackground = new QLabel(this);
	m_lblBlueBackground = new QLabel(this);
	switch (m_layout)
	{
	case k_layout_6:
		RIF(true!= m_iniLayout.setProperties(this, k_ini_sec_queuead));
		RIF(true!= m_iniLayout.setProperties(m_lbQueue, k_ini_sec_queuebody));
		RIF(true!= m_iniLayout.setProperties(m_lbLogo, k_ini_sec_logo));
		RIF(true!= m_iniLayout.setProperties(m_lbSmallQueue, k_ini_sec_smallqueue));
		RIF(true!= m_iniLayout.setProperties(m_lblGreenBackground, k_ini_sec_layout6_green_bg));
		RIF(true!= m_iniLayout.setProperties(m_lblBlueBackground, k_ini_sec_layout6_blue_bg));
		m_lbQueue->setAutoFillBackground(false);
		m_lbLogo->setAutoFillBackground(false);
		m_lbLogo->show();
		m_lbLogo->raise();
		m_lbSmallQueue->setAutoFillBackground(false);
		m_lbLogo->setPixmap(QPixmap("logo.bmp"));
		break;
	case k_layout_7:  
		RIF(true!= m_iniLayout.setProperties(this, k_ini_sec_fullscreen));
		RIF(true!= m_iniLayout.setProperties(m_lbQueue, k_ini_sec_queuebody));
		RIF(true!= m_iniLayout.setProperties(m_lblGreenBackground, k_ini_sec_layout7_green_bg));
		RIF(true!= m_iniLayout.setProperties(m_lblBlueBackground, k_ini_sec_layout7_blue_bg));
		m_lbQueue->setAutoFillBackground(false);
		break;
	case k_layout_8:
	case k_layout_9:
	case k_layout_10:
		RIF(true!= m_iniLayout.setProperties(this, k_ini_sec_independentqueuewidget));
		RIF(true!= m_iniLayout.setProperties(m_lbQueue, k_ini_sec_independentqueuebody));
		RIF(true!= m_iniLayout.setProperties(m_lblGreenBackground, k_ini_sec_independentgreenbg));
		RIF(true!= m_iniLayout.setProperties(m_lblBlueBackground, k_ini_sec_independentbluebg));
		m_lbQueue->setAutoFillBackground(false);
		break;
	default:
		LOG_E("should not be here");
	}
	this->show();
	return 0;
}/* }}} */

void XQueueWidget::resumeWidget () /*  {{{ */
{
//	m_timer->stop();
	m_lblGreenBackground->hide();
	m_lblBlueBackground->show();
	m_counter = 0;
	if(k_layout_6 == m_layout) {
		m_lbSmallQueue->hide();
		m_lbLogo->show();
	}
	//this->show();
} /* }}} */

void XQueueWidget::keepSmallQueue()  /* {{{ */
{
//	m_timer->stop();
	m_lblGreenBackground->hide();
	m_lblBlueBackground->show();
	m_counter = 0;
	if(k_layout_6 == m_layout) {
		m_lbSmallQueue->show();
		m_lbLogo->hide();
	}
	//this->show();
} /* }}} */

void XQueueWidget::showQueue(uint32_t in_queueNumber) /* {{{ */
{
	this->show();
	if (k_layout_6 == m_layout) {
		m_lbLogo->hide();
		m_lbSmallQueue->setText(QString::number(in_queueNumber));
		m_lbSmallQueue->raise();
		m_lbSmallQueue->show();
	}
	QString strQueue = QString::number(in_queueNumber);
	strQueue = strQueue.rightJustified(4,'0');
	strQueue = QString(k_path_wav) + QString("Q") + strQueue + QString(".wav");
	QFile checkFile;
	checkFile.setFileName(strQueue);
	if(!checkFile.exists()) {
		strQueue = QString(k_path_wav) + QString("QDefault.wav");
	}
	QStringList arguments;
	arguments << strQueue;
	m_proc.start(k_aplayer, arguments);
	LOG_T("show Queue now");
	this->show();
	m_queueNumber = in_queueNumber;
	m_lbQueue->setNum((int)m_queueNumber);
	m_lbQueue->show();
	m_counter = 0;
	m_timer->start(1000);
	return;
} /* }}} */

void XQueueWidget::slotSwitchBackground()
{
	LOG_T("Switch Background Enter");
	//Repaint
	LOG_T("Repaint Queue Widget");
	this->repaint();
	if (m_counter < 30) {
		m_counter++;
		if (m_counter%2==0) {
			LOG_T("Show Blue");
			m_lblGreenBackground->hide();
			m_lblBlueBackground->show();
		}
		else {
			LOG_T("Show Green");
			m_lblBlueBackground->hide();
			m_lblGreenBackground->show();
		}
		m_lbQueue->raise();
		m_lbQueue->show();
	} else {
//		LOG_T("---> Queue Play Over");
		if (m_layout == k_layout_8 || m_layout == k_layout_9) {
			m_lbQueue->hide();
		}
		m_counter = 0;
		m_timer->stop();
		emit sgShowQueueDone();
	}
}

void XQueueWidget::stopQueue()
{
//	LOG_T("stop Queue");
	m_timer->stop();
	m_counter = 0;

	// resume background
	m_lblGreenBackground->hide();
	m_lblBlueBackground->show();
	m_lbQueue->hide();
}

void XQueueWidget::showWMLogo()
{
	m_lbQueue->hide();   // WM1 & WM2
	m_lbSmallQueue->hide();//WM1
	m_lbSmallQueue->hide();
	m_lbLogo->show();  // WM1
}

void XQueueWidget::doHalt()
{
	this->stopQueue();
	if (m_layout == k_layout_6) {  //
		m_lbQueue->hide();
		m_lbSmallQueue->hide();
		m_lbLogo->show();
	}

	if (m_layout == k_layout_8 || m_layout == k_layout_9) {
		m_lblGreenBackground->hide();
		m_lblBlueBackground->show();
		m_lbQueue->hide();
	}

	if (m_layout == k_layout_7 || m_layout == k_layout_10){
		m_lblGreenBackground->hide();
		m_lblBlueBackground->show();
		m_lbQueue->hide();
	}

	this->hide();
}

void XQueueWidget::doResume()
{
	m_timer->stop();
	m_counter = 0;
	if (m_layout == k_layout_6) {
		m_lbQueue->hide();
		m_lbSmallQueue->hide();
		m_lbLogo->show();
	}
	if (m_layout == k_layout_10 || m_layout == k_layout_7){
		m_lblGreenBackground->hide();
		m_lblBlueBackground->show();
		m_lbQueue->hide();
	}

	this->show();
}
